package mblog.core.persist.dao.impl;

import mblog.core.persist.dao.PostKeywordDao;
import mblog.core.persist.entity.PostKeywordPO;
import mtons.modules.annotation.Repository;
import mtons.modules.persist.impl.BaseRepositoryImpl;
import org.hibernate.Query;

import java.util.List;

@Repository(entity = PostKeywordPO.class)
public class PostKeywordDaoImpl extends BaseRepositoryImpl<PostKeywordPO> implements PostKeywordDao {

    @Override
    public List<PostKeywordPO> hotKeywords() {
        Query query = createQuery("from PostKeywordPO order by amount desc");
        query.setMaxResults(10);
        return query.list();
    }

    @Override
    public PostKeywordPO findByKeyword(String keyword) {
        Query query = createQuery("from PostKeywordPO where keyword =:keyword");
        query.setString("keyword", keyword);
        query.setMaxResults(1);
        List list = query.list();
        if (list != null && !list.isEmpty()) {
            return (PostKeywordPO) list.get(0);
        }
        return null;
    }

}
